
//-------------------------------------------------------------
//-------------------------------------------------------------
user_list.Instances = null;
//-------------------------------------------------------------
user_list.CreateObject = function(config) {
    var obj = null;
    if (user_list.Instances != null) {
        obj = user_list.Instances[config.id];
    }
    if (obj == null) {
        if (user_list.Instances == null) {
            user_list.Instances = new Object();
        }
        obj = new user_list(config);
        user_list.Instances[config.id] = obj;
    }
    obj.init();

    return obj;
}
//-------------------------------------------------------------
function user_list(config) {
    var Me = this;
    this.id = config.id;
    this.FOLDER_UPLOAD_TEMP = config.FOLDER_UPLOAD_TEMP;
    this.itemModel = null;
    this.full_url = config.full_url;
    //------------------------------------------------------------
    this.init = function() {
        Me.build_event();
        jQuery("#cmdSort,.active-check,#cmdSearch").click(function() {
            Me.get_data(1);
        });
        jQuery("#usertype,#location_id").change(function() {
            Me.get_data(1);
        });
        jQuery("#frmlist").submit(function() {
            Me.get_data(1);
        })
    }

    this.build_event = function() {
        jQuery(".paging-set").unbind('click').bind('click', Me.paging_set_click);
        jQuery(".classDelete").unbind('click').bind('click', Me.classDelete_click);
        jQuery(".classEdit").unbind('click').bind('click', Me.classEdit_click);
        jQuery(".classViewDetail").unbind('click').bind('click', Me.classViewDetail_click);
    }

    this.classViewDetail_click = function() {
        var id = jQuery(this).attr('itemid');
        _ajax.url = url_base + "admin/user/detail?id=" + id;
        _ajax.loading(true);
        _ajax.data = '';
        _ajax.method = 'GET';
        _ajax.success = function(data) {
            if (data.error) {
                show_msg_error(data.msg);
            } else {
                jQuery('#divList').hide();
                jQuery('#divEdit').hide();
                jQuery('#divViewDetail').show();
                jQuery('#divViewDetail').html(data.msg);
                jQuery(".classEdit").unbind('click').bind('click', Me.classEdit_click);
            }
            _ajax.loading(false);
        }
        _ajax.show();
    }

    this.classEdit_click = function() {
        var id = jQuery(this).attr('itemid');
        _ajax.url = url_base + "admin/user/edit?id=" + id;
        _ajax.loading(true);
        _ajax.data = '';
        _ajax.method = 'GET';
        _ajax.success = function(data) {
            if (data.error) {
                show_msg_error(data.msg);
            } else {
                jQuery('#divList').hide();
                jQuery('#divEdit').show();
                jQuery('#divViewDetail').hide();
                jQuery('#divEdit').html(data.msg);
                jQuery(".cmdCancelEdit").unbind('click').click(function() {
                    Me.back_list();
                });
            }
            _ajax.loading(false);
        }
        _ajax.show();
    }

    this.back_list = function() {
        Me.get_data(this.page, function() {
            jQuery('#divList').show();
            jQuery('#divEdit').hide();
        });
    }

    this.back_list_detail = function() {
        jQuery('#divList').show();
        jQuery('#divViewDetail').hide();
    }

    this.classDelete_click = function() {
        var itemid = jQuery(this).attr('itemid');
        jQuery("#modal-delete").modal('show');
        jQuery("#cmdDelete").unbind('click').click(function() {
            jQuery("#modal-delete").modal('hide');
            _ajax.loading(true);
            _ajax.url = url_base + "admin/user/deleted";
            _ajax.data = "id=" + itemid;
            _ajax.method = 'POST';
            _ajax.success = function(data) {
                _ajax.loading(false);
                Me.get_data(1);
                if (data.error == 1) {
                    show_msg_error(data.msg);
                } else {
                    show_msg_success(data.msg);
                }

            }
            _ajax.show();
        });
    }

    this.page = 1;
    this.get_data = function(page, success) {
        Me.page = page;
        _ajax.url = url_base + "admin/user/get_data" + "?page=" + page;
        _ajax.data = jQuery("#frmlist").serialize();
        _ajax.loading(true);
        _ajax.method = 'POST';
        _ajax.success = function(data) {
            _ajax.loading(false);
            if (Me.itemModel == null) {
                jQuery(".tr-show").show();
                jQuery("#list-table-1 tbody tr").each(function(i) {
                    if (i > 0) {
                        jQuery(this).remove();
                    }
                });
                Me.itemModel = new ItemModel();
                ko.applyBindings(Me.itemModel);
            }

            Me.itemModel.loadItem(data.data, data.pagination, data.title, function() {
                Me.build_event();
            });
            if (typeof(success) == 'function') {
                success();
            }
        }
        _ajax.show();
    }

    this.paging_set_click = function() {
        var page = jQuery(this).attr('page');
        Me.get_data(page);
    }

}


